<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Subsidy extends Model
{
    //
    protected $table = 'subsidy';

//    里程数限制
    protected $limitDistance = 6;
//每页数量限制
    protected $pageSize;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->pageSize = env('pagesize', 10);
    }

    /**
     * 获取列表
     * @param int $category
     * @param int $sort
     * @param int $recommend
     * @param string $title
     * @param int $page
     * @param string $lat
     * @param string $lnt
     */
    public function getSub(int $category, int $sort, string $title, int $recommend, int $page, string $lat, string $lnt, int $size)
    {

        $this->pageSize = !$size ? $this->pageSize : $size;
        $where = [
            ['s1.status','=', '1'],
            ['s1.is_frozen','=', '0'],
            ['s1.current_rest', '>', 0],
        ];

//        是否推荐
        if( $recommend > 0 ){
            $where[] = ['s1.is_recommend', '=', 1];
        }
//        标题
        if( $title != '' ){
            $where[] = ['si.title', 'like', "%$title%"];
        }
        $data = [];
        $Model = \DB::table('subsidy as s1')
            ->join('stores_info as si', 's1.store_info_id', '=', 'si.id')
            ->join('stores as s', 'si.store_id', '=', 's.id')
            ->where($where)->select(['s.*', 's1.id as sub_id', 's1.max_money', 's1.return_money', 's1.current_rest', 's1.return_rate', 'si.type as plat_type', 'si.title']);

        //        分类
        if( $category > 0 ){
            $Model->whereRaw('find_in_set("'.$category.'", s.cate_ids)');
        }
        switch ($sort){
//            距离排序
            case 0:
                $list = $Model->get()->toArray();
                foreach ( $list as &$item ){
                    $item->distance = getDistance($lat, $lnt, $item->lat, $item->lnt);
                    if( $recommend == 0 || $item->distance <= $this->limitDistance ){
                        $data[] = (Array)$item;
                    }
                }
                $last_names = array_column($data,'distance');
                array_multisort($last_names,SORT_ASC,$data);
                $data = array_slice($data, ($page -1 )*$this->pageSize, $this->pageSize);
                break;
            case 1:
                $list = $Model->orderBy('s1.return_money', 'desc')->offset(($page -1 )*$this->pageSize)->limit($this->pageSize)->get()->toArray();
                foreach ( $list as &$item ){
                    $item->distance = getDistance($lat, $lnt, $item->lat, $item->lnt);
                    $data[] = (Array)$item;
                }
                break;
            case 2:
                $list = $Model->orderBy('s1.return_rate', 'desc')->offset(($page -1 )*$this->pageSize)->limit($this->pageSize)->get()->toArray();
                foreach ( $list as &$item ){
                    $item->distance = getDistance($lat, $lnt, $item->lat, $item->lnt);
                    $data[] = (Array)$item;
                }
                break;
            case 3:
                $list = $Model->where('s1.good_comment_type', 0)->get()->toArray();
                foreach ( $list as $item ){
                    $item->distance = getDistance($lat, $lnt, $item->lat, $item->lnt);
                    $data[] = (Array)$item;
                }
                $last_names = array_column($data,'distance');
                array_multisort($last_names,SORT_ASC,$data);
                $data = array_slice($data, ($page -1 )*$this->pageSize, $this->pageSize);
                break;
            default:
                break;
        }
        return $data;

    }

    public function storesInfo()
    {
        return $this->hasOne('App\StoresInfo', 'id', 'store_info_id');
    }
    public function stores()
    {
        return $this->hasOne('App\Store', 'id', 'store_id');
    }
    public function merchant(): \Illuminate\Database\Eloquent\Relations\HasOne
    {
        return $this->hasOne('App\User', 'id', 'uid');
    }
}
